from aiogram import types
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from weed_shop.infrastructure.database.services import DeliveryZoneService, UserService
from weed_shop.tg_bot.services.tg_helpers import answer_msg_with_autodelete
from weed_shop.tg_bot.states import ClientSG


async def not_confirm_age(call: types.CallbackQuery, _, manager: DialogManager):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    await answer_msg_with_autodelete(call.message, locale.get("verify-age-to-use-bot"))


async def verify_user_age(call: types.CallbackQuery, _, manager: DialogManager):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    user_service: UserService = manager.middleware_data.get("user_service")

    await user_service.update_user_is_adult(call.from_user.id)

    await call.answer(locale.get("age-verified-msg"))

    await manager.next()


async def select_delivery_zone(call: types.CallbackQuery, _, manager: DialogManager, delivery_zone_id: str):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    user_service: UserService = manager.middleware_data.get("user_service")
    delivery_zone_service: DeliveryZoneService = manager.middleware_data.get("delivery_zone_service")

    await user_service.update_user_delivery_zone(call.from_user.id, int(delivery_zone_id))
    delivery_zone = await delivery_zone_service.get_delivery_zone_by_id(int(delivery_zone_id))

    await call.answer(locale.get("your-delivery-zone", delivery_zone=delivery_zone.title))

    await manager.switch_to(ClientSG.start)
